#import "Serial_Controller.h"

#define k1a  5
#define k1b  3
#define k2a  4
#define k2b  2
#define k3a  4
#define k3b  3
#define k4a  5
#define k4b  2

@implementation Serial_Controller

- (void)awakeFromNib{
	
	encodingArray = [NSArray arrayWithObjects:@"1",@"a",@"b",@"c", @"Q", @"R", @"S", @"8",@"9", @"T", @"U", @"V", @"Z",@"d",@"e",@"2",@"3",@"4",@"f",@"g",@"r",@"s",@"t",@"u",@"v",@"z",@"5",@"6",@"7",@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H",@"I",@"h",@"i",@"l",@"m",@"n",@"o",@"p",@"q",@"L",@"M", @"N", @"O", @"P", nil];
	
	[encodingArray retain];
	usagePath = [[NSBundle mainBundle] pathForResource:@"usage" ofType:@"txt"];
	
	NSArray *array = [[NSString stringWithContentsOfFile:usagePath] componentsSeparatedByString:@"-"];
	
	NSString *verifyTxt = [array objectAtIndex: 0];
	
	if([verifyTxt isEqualToString:@"=== NO BLOCK ==="]){
		
		[graphicalTimes setIntValue: 10];
		
		[times setStringValue:@"Correct serial number."];
		
		[calcPanel makeKeyAndOrderFront: self];
		
	}
	
	else{
		
		[serialPanel makeKeyAndOrderFront: self];
		
		int usage = [[array objectAtIndex: 0] intValue];
		
		if(usage == 0){
			[times setStringValue:@"You can't use this software. Please add a valid serial number."];
			[graphicalTimes setIntValue: 0];
		}
		
		else{
			usage--;
			if(usage == 0){
				[times setStringValue:@"You can't use this software. Please add a valid serial number."];
				[graphicalTimes setIntValue: 0];
			}
			else{
				[times setStringValue:[NSString stringWithFormat:@"You can use this software %d times", usage]];
				NSString *newTxt = [NSString stringWithFormat:@"%d", usage];
				[newTxt writeToFile: usagePath atomically:YES encoding: NSASCIIStringEncoding error: NULL];
				[graphicalTimes setIntValue: usage];
			}
		}
	}
	
	[usagePath retain];
	
}




- (IBAction)newSerial:(id)sender {
    
	[DEVserial setStringValue:[self newSerialString]];
}

- (IBAction)next:(id)sender {
	
	[calcPanel makeKeyAndOrderFront: self];
	[serialPanel orderOut: self];
	
}

- (IBAction)set0:(id)sender {
    
	NSString *verifyKey = [NSString stringWithFormat:@"0"];
	
	[verifyKey writeToFile: usagePath atomically: YES encoding: NSASCIIStringEncoding error: NULL];		[self awakeFromNib];
	
}

- (IBAction)set10:(id)sender {
	
	[calcPanel orderOut: self];
	
	[serialPanel makeKeyAndOrderFront: self];
	
	NSString *verifyKey = [NSString stringWithFormat:@"11"];
	
	[verifyKey writeToFile: usagePath atomically: YES encoding: NSASCIIStringEncoding error: NULL];		[self awakeFromNib];
	
}

- (IBAction)submit:(id)sender {
	
	BOOL verifica;
	
	if([[serial stringValue] length] == 13){
		
		NSMutableArray *mArray = [NSMutableArray array];
		
		for(int i=0;i<13;i++){
			[mArray addObject:[NSString stringWithFormat:@"%c", [[serial stringValue] characterAtIndex: i]]];
		}
		
		int q = [encodingArray indexOfObject:[mArray objectAtIndex: 0]];
		
		int r = [encodingArray indexOfObject:[mArray objectAtIndex: 1]];
		
		int u = [encodingArray indexOfObject:[mArray objectAtIndex: 2]];
		
		int p = [encodingArray indexOfObject:[mArray objectAtIndex: 3]];
		
		int w = [encodingArray indexOfObject:[mArray objectAtIndex: 4]];
		
		int t = [encodingArray indexOfObject:[mArray objectAtIndex: 5]];
		
		int i = [encodingArray indexOfObject:[mArray objectAtIndex: 6]];
		
		int e = [encodingArray indexOfObject:[mArray objectAtIndex: 7]];
		
		int y = [encodingArray indexOfObject:[mArray objectAtIndex: 8]];
		
		int l = [encodingArray indexOfObject:[mArray objectAtIndex: 9]];
		
		int a = [encodingArray indexOfObject:[mArray objectAtIndex: 10]];
		
		int s = [encodingArray indexOfObject:[mArray objectAtIndex: 11]];
		
		int d = [encodingArray indexOfObject:[mArray objectAtIndex: 12]];
		
		verifica = NO;
		
		if(r == (q*k1a)){
			if( u == (r/k1b)){
				if(p == (u+k1a)){
					if(t == (w*k2a)){
						if(i == (t/k2b)){
							if(y == (e*k3a)){
								if(l == (y/k3b)){
									if(s == (a*k4a)){
										if(d == (s/k4b)){
											verifica = YES;
										}		
									}
								}
							}
						}
					}
				}
			}
		}
		
		
		if(verifica){
			[nextButton setEnabled: YES];
			NSString *verifyKey = [NSString stringWithFormat:@"=== NO BLOCK ==="];
			[verifyKey writeToFile: usagePath atomically: YES encoding: NSASCIIStringEncoding error: NULL];
		}
		else{
		
			[serial setStringValue:@"Invalid serial number!"];
			
		}
	}
	
}


- (NSString *)stringFromInt:(int)integer{
	
	return [encodingArray objectAtIndex: integer];
	
}

- (NSString *)newSerialString{
	
	int q, w, e, r, t, y, u, i, l, p, a, s, d;
	q = arc4random() % 9;
	
	r = q * k1a;
	
	u = r / k1b;
	
	p = u + k1a;
	w = arc4random() % 9;
	
	t = w * k2a;
	
	i = t / k2b;
	e = arc4random() % 9;
	
	y = e * k3a;
	
	l = y / k3b;
	a = arc4random() % 9;
	
	s = a * k4a;
	
	d = s / k4b;
	
	return [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@%@%@", [self stringFromInt: q], [self stringFromInt:r], [self stringFromInt: u], [self stringFromInt:p], [self stringFromInt: w], [self stringFromInt: t], [self stringFromInt: i],[self stringFromInt: e], [self stringFromInt: y], [self stringFromInt: l], [self stringFromInt: a], [self stringFromInt:s], [self stringFromInt: d]];
	
}

- (IBAction)sum:(id)sender{
	[sum setIntValue:([first intValue]+[second intValue])];
	
}

@end
